package xFGE.xDisplay.BitmapEx
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import xFGE.xDisplay.SpriteAssistBase;

	

	public class BitmapSprite extends SpriteAssistBase
	{
		/**
		 *纹理范围 
		 */
		protected var _textureRect:Rectangle;
		
		/**
		 *纹理在sprite中的位置 
		 */
		protected var _destPoint:Point;
		
		/**
		 * 热点，中心点，注册点 
		 */
		//protected var _hotPoint:Point;
		
		/**
		 * 纹理 
		 */
		protected var _texture:BitmapData;
		
		/**
		 * 精灵的真正位置 
		 */
		//protected var _spritePos:Point;
		
		/**
		 * 位图数据 
		 */
		protected var _bitmap:Bitmap;
		
		/**
		 * 图片精灵创建 
		 * @param x 纹理开始x坐标
		 * @param y 纹理开始y坐标
		 * @param w 纹理范围宽
		 * @param h 纹理范围高
		 * @param texture 纹理
		 * @param transparent 是否透明
		 * @param color 纹理填充颜色
		 * @param pixelSnapping 是否紧贴像素
		 * @param smoothing 是否抗齿
		 * 
		 */
		public function BitmapSprite(x:int=0,y:int=0,w:int=1,h:int=1,texture:BitmapData=null,
																				 transparent:Boolean=true,
																				 color:int = 0x00000000,
																				 pixelSnapping:String="auto",
																				 smoothing:Boolean=false)
		{
			//TODO: implement function
			super();
			_bitmap = new Bitmap(new BitmapData(w,h,transparent,color), pixelSnapping, smoothing);
			addChild(_bitmap);
			_textureRect = new Rectangle(x,y,w,h);
			_destPoint = new Point(0,0);
			
			if(texture != null){
				_texture = texture;
				SetTextureRect(x,y,w,h);
			}
		}
		
		/**
		 * 移动(该移动涉及中心点,和bitmap中x，y属性不一样） 
		 * @param pos 坐标
		 * 
		 */
		public function MoveTo(pos:Point):void
		{
			x=pos.x - _hotPoint.x;
			y=pos.y - _hotPoint.y;
			_spritePos = pos;
		}
		/**
		 * 设置纹理 
		 * @param texture
		 * 
		 */
		public function SetTexture(texture:BitmapData):void
		{
			_texture = texture;
			_bitmap.bitmapData = texture;
			SetTextureRect(0,0,_texture.width,_texture.height);
		}
		/**
		 * 设置纹理范围 
		 * @param x 纹理起始坐标x
		 * @param y 纹理起始坐标y
		 * @param w 纹理宽
		 * @param h 纹理高
		 * 
		 */
		public function SetTextureRect(x:int,y:int,w:int,h:int):void
		{
			_textureRect.x = x;
			_textureRect.y = y;
			_textureRect.width = w;
			_textureRect.height = h;
			draw();
		}
		
		/**
		 * 设置叠加颜色 
		 * @param color
		 * 
		 */
		/* public function SetColor(color:int):void
		{
			var alpha:Number = Math.round((color >> 24 & 0xff)/255 * 100)/100;
			var red:Number = Math.round((color >> 16 & 0xff)/255 * 100)/100;
			var green:Number = Math.round((color >> 8 & 0xff)/255*100)/100;
			var blue:Number = Math.round((color&0xff)/255*100)/100;
			this.transform.colorTransform = new ColorTransform(red,green,blue,alpha);
		} */
		
		/**
		 *设置旋转，涉及中心点(与bitmap属性中的rotation 不一样) 
		 * @param rotation 选择角度
		 * 
		 */
		/* public function SetRotation(rotation:Number):void
		{
			var oldPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			this.rotation = rotation;
			var newPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			x+=oldPos.x - newPos.x;
			y+=oldPos.y - newPos.y;
			
		} */
		
		/**
		 *  设置放大缩小，涉及中心点(与bitmap属性中的scalex，scaley不一样)
		 * @param scaleX 横坐标放大量
		 * @param scaleY 纵坐标放大量
		 * 
		 */
		/* public function SetScale(scaleX:Number,scaleY:Number):void
		{
			var oldPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			this.scaleX = scaleX;
			this.scaleY = scaleY;
			var newPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			x+=oldPos.x - newPos.x;
			y+=oldPos.y - newPos.y;
		} */
		
		/**
		 * 设置热点（中心点或注册点) 
		 * @param hotPoint
		 * 
		 */
		/* public function SetHotPoint(hotPoint:Point):void
		{
			var oldPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			_hotPoint = hotPoint;
			var newPos:Point = this.parent.globalToLocal(localToGlobal(_hotPoint));
			x+= oldPos.x - newPos.x;
			y+= oldPos.y - newPos.y;
		} */
		/**
		 * 翻转 
		 * @param x -1时为水平翻转
		 * @param y -1时为垂直翻转
		 * 
		 */
	/* 	public function Flip(x:int =1 ,y:int = 1):void
		{
			SetScale(scaleX*x,scaleY*y);
		} */
		
		/**
		 * 像素拷贝 
		 * 
		 */
		protected function draw():void
		{
			//trace(_textureRect);
			_bitmap.bitmapData.copyPixels(_texture,_textureRect,_destPoint);
		}
		
		/**
		 * 获取bitmap 
		 * @return 
		 * 
		 */
		/* public function get bitmap():Bitmap
		{
			return _bitmap;
		} */
		/**
		 * 获取真正的sprite坐标 
		 * @return 
		 * 
		 */
		/* public function get spritePos():Point
		{
			return _spritePos;
		} */
		public function clean():void
		{
			this._bitmap.bitmapData.fillRect(new Rectangle(0,0,_bitmap.width,_bitmap.height),0x00ffffff);
		}
	}
}